#include "DS1307.h"

uint8_t tempdata[7]={0x23,0x12,0x07,0x16,0x35,0x20,0x04};

//写一字节到对应地址去
unsigned char Write1307(unsigned char add, unsigned char dat) 
{
    unsigned char temp;
    
    temp = ((dat / 10) << 4) | (dat % 10);// 转BCD码再写入1307
    IIC_Start();
    IIC_Send_Byte(0xd0);
    IIC_Wait_Ack();
    IIC_Send_Byte(add);
    IIC_Wait_Ack();
    IIC_Send_Byte(temp);
    IIC_Wait_Ack();
    IIC_Stop();
    return (0);
}

//读一字节
unsigned char Read1307(unsigned char add)
{
    unsigned char dat;
    IIC_Start();
    IIC_Send_Byte(0xD0);
    IIC_Wait_Ack();
    IIC_Send_Byte(add);
    IIC_Wait_Ack();
    IIC_Stop();
    IIC_Start();
    IIC_Send_Byte(0xD1);
    IIC_Wait_Ack();
    dat = IIC_Read_Byte(0x00); //地址0x00
    IIC_NAck();
    IIC_Stop();
    return dat;
}

//BCD转BIN函数
uint8_t BCD2BIN(char bcd)
{
    return (0xff & (bcd>>4))*10 +(0xf & bcd);
}

//读1307时间，*data：读缓存 ,data_len:缓存长度  ,data_type:0:hex模式   1：dec模式
void Read_1307_time(uint8_t *data,uint8_t data_len,uint8_t data_type)
{
  if((data_len!=7))
  {
    // DEBUG_ASP("error -1 +%d\r\n",data_len);
    return ;
  }
  else if(data_type==0)//十六进制格式
  {
    data[0] = (Read1307(0x06) & 0xff);//年低2位
    data[1] = (Read1307(0x05) & 0x1F);//月
    data[2] = (Read1307(0x04) & 0x3F);//日
    data[3] = (Read1307(0x02) & 0x3F);//时
    data[4] = (Read1307(0x01) & 0x7F);//分
    data[5] = (Read1307(0x00) & 0x7F);//秒
    data[6] = (Read1307(0x03) & 0x07);//星期
    // DEBUG_ASP("TIME(Hex): 20%02x-%02x-%02x %02x:%02x:%02x ,%02x\r\n", data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
  }
  else//十进制模式
  {
    data[0] = BCD2BIN(Read1307(0x06) & 0xff);//年低2位
    data[1] = BCD2BIN(Read1307(0x05) & 0x1F);//月
    data[2] = BCD2BIN(Read1307(0x04) & 0x3F);//日
    data[3] = BCD2BIN(Read1307(0x02) & 0x3F);//时
    data[4] = BCD2BIN(Read1307(0x01) & 0x7F);//分
    data[5] = BCD2BIN(Read1307(0x00) & 0x7F);//秒
    data[6] = BCD2BIN(Read1307(0x03) & 0x07);//星期
    // DEBUG_ASP("TIME(Dec): 20%02d-%02d-%02d %02d:%02d:%02d T:%02d\r\n", data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
  }
}

//写1307时间，*data：写缓存 ,data_len:缓存长度  ,data_type:0:hex模式   1：dec模式
void Write_1307_time(uint8_t *data,uint8_t data_len,uint8_t data_type)
{ 
  if((data_len!=7))
  {
    // DEBUG_ASP("error -1 +%d\r\n",data_len);
    return ;
  }
  else if(data_type==0)//十六进制格式
  {
    Write1307(0x06, BCD2BIN(data[0]));//年
    Write1307(0x05, BCD2BIN(data[1]));//月
    Write1307(0x04, BCD2BIN(data[2]));//日
    Write1307(0x02, BCD2BIN(data[3]));//时
    Write1307(0x01, BCD2BIN(data[4]));//分
    Write1307(0x00, BCD2BIN(data[5]));//秒
    Write1307(0x03, BCD2BIN(data[6]));//星期
  }
  else//十进制模式
  {
    Write1307(0x06, data[0]);//年
    Write1307(0x05, data[1]);//月
    Write1307(0x04, data[2]);//日
    Write1307(0x02, data[3]);//时
    Write1307(0x01, data[4]);//分
    Write1307(0x00, data[5]);//秒
    Write1307(0x03, data[6]);//星期
  }
}

void DS1307_init(void)
{
    IIC_Init(); 
	delay_ms(200);
	// BCD ---->>>>>>>>>> 0x0002 0003, 0x0001 0002 ...
	// uint8_t tempdataBCD[7]={0x23,0x12,0x07,0x16,0x35,0x20,0x04};
	// 
	uint8_t tempdataBCD[7] = {0x25, 0x01, 0x08, 0x22, 0x11, 0x20, 0x03};
	Write_1307_time(tempdataBCD,sizeof(tempdataBCD)/sizeof(*(tempdataBCD)),0);
}

void DS1207_TEXT(void)
{
  Read_1307_time(tempdata,sizeof(tempdata)/sizeof(*(tempdata)),1);
  // DEBUG_ASP("TIME0: 20%02x-%02x-%02x %02x:%02x:%02x ,%02x\r\n", tempdata[0], tempdata[1], tempdata[2], tempdata[3], tempdata[4], tempdata[5], tempdata[6]);
}
